
package com.sun.alen.service.Impl;

import com.sun.alen.entity.*;
import com.sun.alen.mapper.MainClassMapper;
import com.sun.alen.mapper.ProfessionMapper;
import com.sun.alen.mapper.TeacherMapper;
import com.sun.alen.service.MainClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MainClassServiceImpl implements MainClassService {
    @Autowired
    private MainClassMapper mainClassMapper;

    @Autowired
    private ProfessionMapper professionMapper;

    @Autowired
    private TeacherMapper teacherMapper;
    @Override
    public boolean add(MainClass mainClass) {
        mainClass.setClassNumber(0);
        return mainClassMapper.add(mainClass);
    }

    @Override
    public boolean update(MainClass mainClass) {
        return mainClassMapper.update(mainClass);
    }

    @Override
    public boolean delete(String id) {
        return mainClassMapper.delete(id);
    }

    @Override
    public PageBean search(String input, String select, Integer currentPage, Integer pageSize) {
        Integer start= (currentPage-1)*pageSize;
        int endIndex = 0;
        List<MainClass> mainClassList=new ArrayList<>();
        long count=0;
        input="%"+input+"%";
        if(input.equals("%%")){
            mainClassList=mainClassMapper.list(start,pageSize);
            count=mainClassMapper.count();
        }else{
            switch (select) {
                case "1" -> {
                    mainClassList = mainClassMapper.searchByName(input, start, pageSize);
                    count = mainClassMapper.countByName(input);
                }
                case "2" -> {
                    List<Profession> professionList = professionMapper.searchIdByName(input);
                    for (int i = 0; i < professionList.size(); i++) {
                        List<MainClass> tempList = mainClassMapper.searchByProfession(professionList.get(i).getProfessionId());
                        mainClassList.addAll(tempList);
                    }
                    endIndex = Math.min(start + pageSize, mainClassList.size());
                    count = mainClassList.size();
                    mainClassList = mainClassList.subList(start,endIndex);
                }
                case "3" ->{
                    List<Teacher> teacherList = teacherMapper.searchIdByName(input);
                    for (int i = 0; i < teacherList.size(); i++) {
                        List<MainClass> tempList = mainClassMapper.searchByTeacher(teacherList.get(i).getTeacherId());
                        mainClassList.addAll(tempList);
                    }
                    endIndex = Math.min(start + pageSize, mainClassList.size());
                    count = mainClassList.size();
                    mainClassList = mainClassList.subList(start,endIndex);
                }
                default -> {
                    mainClassList = mainClassMapper.list(start, pageSize);
                    count = mainClassMapper.count();
                }
            }
        }
        return new PageBean(count,mainClassList);
    }

    @Override
    public List<MainClass> all() {
        return mainClassMapper.all();
    }

    @Override
    public List<MainClass> getMainClassByTea(String id) {
        return mainClassMapper.getMainClassByTea(id);
    }

    @Override
    public PageBean searchTeacher(String input, String select, Integer teacherId, Integer currentPage, Integer pageSize) {
        Integer start = (currentPage - 1) * pageSize;
        int endIndex = 0;
        List<MainClass> mainClassList=new ArrayList<>();
        long count=0;
        input="%"+input+"%";
        switch (select){
            case "1":
                mainClassList.addAll(mainClassMapper.searchByClassName(input,teacherId,start,pageSize));
                count = mainClassMapper.countByClassName(input, teacherId);
                break;
            default:
                mainClassList=mainClassMapper.listByTeacherId(teacherId,start,pageSize);
                count=mainClassMapper.countByTeacherId(teacherId);
                break;
        }
        PageBean pageBean = new PageBean(count,mainClassList);
        return pageBean;
    }
}
